package io.renren.modules.sys.service.impl;

import io.renren.modules.sys.entity.*;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.OrdersDao;
import io.renren.modules.sys.service.OrdersService;


@Service("orderService")
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, OrdersEntity> implements OrdersService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrdersEntity> page = this.page(
                new Query<OrdersEntity>().getPage(params),
                new QueryWrapper<OrdersEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getShelvesOrder(Map<String, Object> params){

        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        String ordernum = null;
        if(params.get("ordernum")!=null&&!params.get("ordernum").equals("")){
            ordernum = params.get("ordernum").toString();
        }

        Integer shelvesid = null;
        if( params.get("shelvesid")!=null&&!params.get("shelvesid").equals("")){
            shelvesid = Integer.parseInt(params.get("shelvesid").toString());
        }

        String buyer = null;
        if(params.get("buyer")!=null&&!params.get("buyer").equals("")){
            ordernum = params.get("buyer").toString();
        }

        String startimeObj = null;
        if(params.get("startime")!=null&&!params.get("startime").equals("")){
            startimeObj = params.get("startime").toString();
        }

        String endtimeObj = null;
        if(params.get("endtime")!=null&&!params.get("endtime").equals("")){
            endtimeObj = params.get("endtime").toString();
        }

        int count = baseMapper.queryListcount(ordernum,shelvesid,buyer,startimeObj,endtimeObj);
        List<ShelvesOrdersEntity> list = baseMapper.getShelvesOrder(ordernum,shelvesid,buyer,startimeObj,endtimeObj,(currPage-1)*pageSize,currPage*pageSize);

        return new PageUtils(list,count,pageSize,currPage);
    }

    @Override
    public ShelvesOrdersStatisticsEntity getStatistics(Map<String, Object> params){
        String ordernum = null;
        if(params.get("ordernum")!=null&&!params.get("ordernum").equals("")){
            ordernum = params.get("ordernum").toString();
        }

        Integer shelvesid = null;
        if( params.get("shelvesid")!=null&&!params.get("shelvesid").equals("")){
            shelvesid = Integer.parseInt(params.get("shelvesid").toString());
        }

        String buyer = null;
        if(params.get("buyer")!=null&&!params.get("buyer").equals("")){
            ordernum = params.get("buyer").toString();
        }

        String startimeObj = null;
        if(params.get("startime")!=null&&!params.get("startime").equals("")){
            startimeObj = params.get("startime").toString();
        }

        String endtimeObj = null;
        if(params.get("endtime")!=null&&!params.get("endtime").equals("")){
            endtimeObj = params.get("endtime").toString();
        }
        ShelvesOrdersStatisticsEntity entity = baseMapper.getStatistics(ordernum,shelvesid,buyer,startimeObj,endtimeObj);
        return entity;
    }

}
